package com.longqin.business.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.longqin.business.config.RequiredPermission;
import com.longqin.business.entity.DesDiagram;
import com.longqin.business.service.IDesDiagramService;
import com.longqin.business.util.ResponseData;
import com.longqin.business.util.ResponseEnum;
import com.longqin.business.util.SessionUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author longqin
 * @since 2024-11-22
 */
@RestController
@RequestMapping("/desDiagram")
@Api(tags="图表设计器")
public class DesDiagramController {

	@Autowired
	IDesDiagramService desDiagramService;
	
	/**
	 * @Description 获取单个图表
	 * @Author longqin
	 * @Time: 2024年11月22日
	 */
	@ApiOperation(value = "获取单个图表", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "图表ID", required = true, dataType = "int") })
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败") })
	@GetMapping("/getById")
	public ResponseData getById(int id) {
		DesDiagram diagram = desDiagramService.getById(id);
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", diagram);
	}
	
	/**
	 * @Description 分页获取图表清单
	 * @Author longqin
	 * @Time: 2024年11月22日
	 */
	@ApiOperation(value = "分页获取图表清单", httpMethod = "GET")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "modelName", value = "图表名称", required = true, dataType = "String"),
		@ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "int"),
		@ApiImplicitParam(name = "size", value = "每页数量", required = true, dataType = "int") })
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败"), @ApiResponse(code = 3, message = "参数错误") })
	@GetMapping("/getDiagramPage")
	@RequiredPermission("diagramList:view")
	public ResponseData getDiagramPage(String modelName, Integer page, Integer size) {
		if (null == page || null == size) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		List<DesDiagram> diagramList = desDiagramService.getPage(modelName, SessionUtil.getSessionUser().getOrganizationId(), (page - 1) * size, size);
		int total = desDiagramService.getCount(modelName, SessionUtil.getSessionUser().getOrganizationId());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", diagramList);
		map.put("total", total);
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", map);
	}
	
	/**
	 * @Description 创建图表
	 * @Author longqin
	 * @Time: 2024年11月22日
	 */
	@ApiOperation(value = "创建图表", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "图表实体项", required = true, dataType = "DesDiagram") })
	@ApiResponses({ @ApiResponse(code = 1, message = "创建成功"), @ApiResponse(code = 0, message = "创建失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/create")
	@RequiredPermission("diagramDesigner:view")
	public ResponseData create(@RequestBody DesDiagram entity) throws Exception {
		if (null == entity || null == entity.getModelName()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		entity.setCreator(SessionUtil.getSessionUser().getUserId());
		entity.setOrganizationId(SessionUtil.getSessionUser().getOrganizationId());
		int result = desDiagramService.create(entity);
		if (result > 0){
			return new ResponseData(ResponseEnum.SUCCESS.getCode(), "创建成功", result);
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "创建失败", result);
		}
	}

	/**
	 * @Description 修改图表
	 * @Author longqin
	 * @Time: 2024年11月22日
	 */
	@ApiOperation(value = "修改图表", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "图表实体项", required = true, dataType = "DesDiagram") })
	@ApiResponses({ @ApiResponse(code = 1, message = "修改成功"), @ApiResponse(code = 0, message = "修改失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/update")
	@RequiredPermission("diagramDesigner:view")
	public ResponseData update(@RequestBody DesDiagram entity) throws Exception {
		if (null == entity || null == entity.getModelName()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		entity.setCreator(SessionUtil.getSessionUser().getUserId());
		entity.setOrganizationId(SessionUtil.getSessionUser().getOrganizationId());
		int result = desDiagramService.update(entity);
		if (result > 0){
			return new ResponseData(ResponseEnum.SUCCESS.getCode(), "修改成功", result);
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "修改失败", result);
		}
	}

	/**
	 * @Description 删除图表
	 * @Author longqin
	 * @Time: 2024年11月22日
	 */
	@ApiOperation(value = "删除图表", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "图表ID", required = true, dataType = "int") })
	@ApiResponses({ @ApiResponse(code = 1, message = "删除成功"), @ApiResponse(code = 0, message = "删除失败") })
	@PostMapping("/delete")
	@RequiredPermission("diagramList:view")
	public ResponseData delete(int id) throws Exception {
		int result = desDiagramService.delete(id);
		if (result >= 0){
			return new ResponseData(ResponseEnum.SUCCESS.getCode(), "删除成功", result);
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "删除失败", result);
		}
	}
	
	/**
	 * @Description 获取用户图表清单
	 * @Author longqin
	 * @Time: 2024年11月26日
	 */
	@ApiOperation(value = "获取用户图表清单", httpMethod = "GET")
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败"), @ApiResponse(code = 3, message = "参数错误") })
	@GetMapping("/getDiagramByCreator")
	@RequiredPermission("diagramDesigner:view")
	public ResponseData getDiagramByCreator() {
		List<DesDiagram> diagramList = desDiagramService.getByCreator(SessionUtil.getSessionUser().getUserId());
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", diagramList);
	}
	
	/**
	 * @Description 获取图表数据源数据
	 * @Author longqin
	 * @Time: 2024年12月4日
	 */
	@ApiOperation(value = "获取图表数据源数据", httpMethod = "GET")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "dataSource", value = "数据源表名", required = true, dataType = "String"),
		@ApiImplicitParam(name = "column", value = "字段名", required = true, dataType = "String") })
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败"), @ApiResponse(code = 3, message = "参数错误") })
	@GetMapping("/getDiagramData")
	public ResponseData getDiagramData(String dataSource, String column) throws Exception {
		if (null == dataSource || null == column) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		String result = desDiagramService.getDiagramData(dataSource, column, SessionUtil.getSessionUser().getOrganizationId());
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", result);
	}
}
